/**
 * 区域管理
 * @author luguoxiang
 * @date 2023-03-27
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { IConfigSchema, isJson, LgDictProps } from '@/soar';
import { basePath } from '@/api/sysConfig';
import { MenuComponent } from '@/types/interface';

const fieldList: IConfigSchema<any>[] = [
  {
    label: '名称',
    field: 'name',
    dataType: 'string',
    table: {
      width: 150,
      order: 0,
    },
    form: {
      rules: true,
    },
  },
  {
    label: '健',
    field: 'code',
    dataType: 'string',
    table: {
      width: 200,
      order: 2,
    },
    form: {
      rules: true,
    },
  },
  {
    label: '值类型',
    field: 'valueType',
    dataType: 'dict',
    params: 'ConfigValueTypeEnum',
    table: {
      width: 120,
      order: 1,
    },
    form: {
      rules: true,
      componentProps: {
        component: 't-radio-group',
      } as LgDictProps,
      colProps: {
        style: 'min-width: 100%;',
      },
    },
  },
  {
    label: '值',
    field: 'value',
    dataType: 'string',
    table: {
      ellipsis: true,
      order: 3,
    },
    form: {
      slots: 'editor',
      rules({ data }) {
        if (data.valueType === 0) {
          return [
            {
              validator: (value) => {
                try {
                  return isJson(value);
                } catch (e) {
                  return false;
                }
              },
              message: '不符合json规范，注意字符串请加双引号',
            },
          ];
        }
        return [];
      },
      formItemProps({ data }) {
        if (data.valueType === 0) {
          return {
            help: '字符串："lg-soar"，数值：2024，布尔值：true，数组：[1,"2",true]，对象：{name:"六哥",age:18}',
          };
        }
        return {};
      },
      colProps: {
        style: 'min-width: 100%;',
      },
    },
  },
  {
    label: '备注',
    field: 'remark',
    dataType: 'string',
    table: {
      order: 4,
      width: 200,
    },
    form: {
      component: 't-textarea',
      colProps: {
        style: 'min-width: 100%;',
      },
    },
  },
];

export default fieldList;

/**
 * 组件配置（用于生成菜单数据）
 */
export const COMPONENTS: MenuComponent[] = [
  {
    title: '系统配置',
    component: () => import('./Index.vue'),
    sort: 30,
    keepalive: true,
    icon: 'setting',
    path: '/system/general/sys-config',
    permits: [`GET${basePath}/query`],
    elements: [
      { title: '新增', code: 'create', permits: [`POST${basePath}/create`] },
      { title: '更新', code: 'update', permits: [`POST${basePath}/update`, `GET${basePath}/detail`] },
      { title: '删除', code: 'remove', permits: [`POST${basePath}/remove`] },
      { title: '清除缓存', code: 'clear-cache', permits: [`POST${basePath}/clear-cache`] },
      { title: 'PC配置', code: 'pc', permits: [`POST${basePath}/set/pc`, `GET${basePath}/pc`] },
      { title: 'APP配置', code: 'app', permits: [`POST${basePath}/set/app`, `GET${basePath}/app`] },
      { title: '登录配置', code: 'login', permits: [`POST${basePath}/set/login`, `GET${basePath}/login`] },
    ],
  },
];
